package org.beans;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyEditorSupport;
import java.util.stream.Stream;

public class BeanInfoDemo {

    public static void main(String[] args) throws IntrospectionException {
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
        Stream.of(beanInfo.getPropertyDescriptors())
                .forEach(e->{
                    //System.out.println(e);
                    Class<?> propertyType = e.getPropertyType();
                    System.out.println(propertyType);
                    String name = e.getName();
                    if("age".equals(name)) {
                        e.setPropertyEditorClass(StringToIntPropertyEditor.class);
                    }
                });
    }

    public static class StringToIntPropertyEditor extends PropertyEditorSupport {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            Integer value = Integer.parseInt(text);
            setValue(value);
        }
    }
}
